﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using Prism.Commands;
using Prism.Events;
using Prism.Ioc;
using Prism.Mvvm;
using Prism.Services.Dialogs;
using WPFNote.Models.Config;
using WPFNote.Utilities;
using WPFNote.Utilities.Common;
using WPFNote.Utilities.Extensions;

namespace WPFNote.ViewModels;

public class ConfigViewModel : BindableBase, IDialogAware
{
    
    /// <summary>
    /// 获取系统自动启动目录
    /// </summary>
    private static string SystemStartPath => Environment.GetFolderPath(Environment.SpecialFolder.Startup);

    /// <summary>
    /// 获取程序完整路径
    /// </summary>
    private static string AppAllPath => Process.GetCurrentProcess().MainModule?.FileName;

    public bool CanCloseDialog()
    {
        return true;
    }

    public void OnDialogClosed()
    {
        Cancel();
    }

    public void OnDialogOpened(IDialogParameters parameters)
    {
        TencentCloudParam = Config.Init().TencentCloudParam ?? new TencentCloudParam();
        BooleanParam = Config.Init().BooleanParam ?? new BooleanParam();
    }

    public string Title { get; set; } = "设置";

    public event Action<IDialogResult> RequestClose;

    private TencentCloudParam _tencentCloudParam = new ();

    public TencentCloudParam TencentCloudParam
    {
        get => _tencentCloudParam;
        set
        {
            _tencentCloudParam = value;
            RaisePropertyChanged();
        }
    }
    
    
    private BooleanParam _booleanParam = new ();

    public BooleanParam BooleanParam
    {
        get => _booleanParam;
        set
        {
            _booleanParam = value;
            RaisePropertyChanged();
        }
    }

    private readonly IEventAggregator _aggregator;

    public DelegateCommand SaveCommand { get; set; }
    public DelegateCommand CancelCommand { get; set; }

    public ConfigViewModel(IContainerProvider containerProvider)
    {
        _aggregator = containerProvider.Resolve<IEventAggregator>();
        SaveCommand = new DelegateCommand(Save);
        CancelCommand = new DelegateCommand(Cancel);
    }


    private void Cancel()
    {
        RequestClose?.Invoke(new DialogResult(ButtonResult.No));
    }

    private void Save()
    {
        Config.Init().BooleanParam = BooleanParam;
        Config.Init().TencentCloudParam = TencentCloudParam;
        SetAutoStart();
        var json = Config.Init().JsonStringify();
        var ok = new BinFileHelper().Write(AppSession.ConfigPath, EncryptionHelper.EncodeBase64(json));
        if (!ok)
        {
            _aggregator.SendMessage("保存失败");
            return;
        }
        _aggregator.SendMessage("保存成功");
        RequestClose?.Invoke(new DialogResult(ButtonResult.OK));
    }

    private void SetAutoStart()
    {
        var quickStart = new QuickStart();
        if (BooleanParam.IsAutoStart)
        {
            // 获取启动路径应用程序快捷方式的路径集合
            List<string> shortcutPaths = quickStart.GetQuickFromFolder(SystemStartPath, AppAllPath);
            //存在2个以快捷方式则保留一个快捷方式-避免重复多于
            if (shortcutPaths.Count >= 2)
            {
                for (int i = 1; i < shortcutPaths.Count; i++)
                {
                    quickStart.DeleteFile(shortcutPaths[i]);
                }
            }
            else if (shortcutPaths.Count < 1)//不存在则创建快捷方式
            {
                quickStart.CreateShortcut(SystemStartPath, AppSession.AppName, AppAllPath, AppSession.AppName);
            }
        }
        else
        {
            //获取启动路径应用程序快捷方式的路径集合
            List<string> shortcutPaths = quickStart.GetQuickFromFolder(SystemStartPath, AppAllPath);
            //存在快捷方式则遍历全部删除
            if (shortcutPaths.Count > 0)
            {
                for (int i = 0; i < shortcutPaths.Count; i++)
                {
                    quickStart.DeleteFile(shortcutPaths[i]);
                }
            }
        }
    }
}